<template>
  <view class="activity-item" @click="toDetails">
    <TImage :src="item.coverUrl" mode="widthFix" width="100%" height="fit-content" />
    <view class="pt-10 pl-10 pr-15">
      <view class="fz-14 fw-7 linh-20 ov-2">{{ item.title }}</view>
      <view class="fz-12 pt-2 times" v-if="item.startTime">
        <text>{{ formatToDateTime(item.startTime, 'YYYY/MM/DD HH:mm') }}</text>
        <text>~</text>
        <text>{{ formatToDateTime(item?.endTime, 'YYYY/MM/DD HH:mm') }}</text>
        <!-- 日期相同 -->
        <!-- <text
          v-if="formatToDateTime(item.startTime, 'YYYY-MM-DD') === formatToDateTime(item.endTime, 'YYYY-MM-DD')">
          {{formatToDateTime(item.endTime,'HH:mm')}}</text> -->
        <!-- 日期不相同 -->
        <!-- <text v-else>{{ formatToDateTime(item.endTime, 'YYYY-MM-DD HH:mm') }}</text> -->
      </view>

      <view class="activity-item-bottom">
        <view v-if="isArray(item.userImgs)" class="user-imgs">
          <template v-for="(v, i) in item.userImgs.splice(0, 3)" :key="i">
            <view class="user-img" :style="{ transform: `translateX(-${i * 50 + '%'})` }">
              <image v-if="v" class="image" :src="v" mode="scaleToFill" />
              <image
                v-else
                class="image"
                src="https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/emptyIcon/empty-avatar.png"
                mode="scaleToFill"
              />
            </view>
          </template>
        </view>
        <view class="will-num">
          <text class="confirmed-number" v-if="item.openRegNum">{{ item.passedNum || 0 }}人已报名</text>
          <text class="remain-number" v-if="item.enrollRestrict">
            剩{{ item.maxNum - item.passedNum < 0 ? 0 : item.maxNum - item.passedNum }}席。
          </text>
        </view>
        <view class="activity-status">
          <ActivityBtnsBtns :item="item" @after-fnc="toDetails" />
        </view>
      </view>
    </view>
    <image
      v-if="item.register?.approvalStatus === 3"
      src="https://hb-saas.obs.cn-south-1.myhuaweicloud.com/mall/saas/icon/activity-icon-1.png"
      class="activity-registered"
    />
  </view>
</template>

<script setup lang="ts">
  import { navigateTo } from '@/router';
  import { type PropType } from 'vue';
  import { type IActivityListResData } from '@/api/activity/types';
  import { formatToDateTime } from '@/utils/dateUtil';
  import TImage from '../TImage/index.vue';
  import ActivityBtnsBtns from './ActivityBtnsBtns.vue';
  import { isArray } from '@/utils/is';

  const props = defineProps({
    item: {
      type: Object as PropType<IActivityListResData>,
      required: true,
    },
  });

  const emit = defineEmits(['refresh']);

  function toDetails() {
    const { id } = props.item;
    navigateTo({
      name: 'activityDetails',
      options: { id, refresh: () => emit('refresh') },
    });
  }
</script>

<style lang="scss" scoped>
  .activity-item {
    background-color: #ffffff;
    border-radius: 20rpx;
    overflow: hidden;
    box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.15);
    position: relative;

    .activity-registered {
      position: absolute;
      left: 0;
      top: 0;
      width: 94rpx;
      height: 94rpx;
      z-index: 99;
    }

    .times {
      color: #aaabab;
    }

    &-bottom {
      height: 100rpx;
      flex-basis: 100rpx;
      display: flex;
      align-items: center;

      .user-imgs {
        display: flex;
        align-items: center;

        .user-img {
          width: 35rpx;
          height: 35rpx;
          border-radius: 50%;
          overflow: hidden;
          border: 2px solid #fff;

          > .image {
            width: 100%;
            height: 100%;
          }
        }
      }

      .confirmed-number {
        font-size: 24rpx;
        color: #808080;
        margin-right: 10rpx;
      }

      .remain-number {
        font-size: 24rpx;
        color: #ef823a;
      }

      .activity-status {
        flex-shrink: 0;
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: flex-end;
      }
    }
  }
</style>
